package newsOrganizer;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndImageImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.List;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import java.util.ArrayList;

/**
 * 
 * @author macitu /
 * 
 * 
 *         /* 2 Required Jar files rome-0.9.jar retrieved from
 *         http://rome.dev.java.net/ jdom.jar retrieved from
 *         http://www.jdom.org/
 */

public class RSSReader {
    
	/**
	 * 
	 * @param rssFeedUrl - the url that we want to read RSS news from 
	 * @return ArrayList of two ArrayList<String> .The fisrt contains titles of news .
	 * The second link to the news (indexes equvalent)
	 * @throws UnknownHostException
	 */
	public static ArrayList iterateRSSFeed(String rssFeedUrl)throws UnknownHostException {
		try {
			// open up a connection to the rss feed

			URLConnection feedUrl = new URL(rssFeedUrl).openConnection();

			// Create Feed Object
			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(feedUrl));

			List list = feed.getEntries();
			List<String> links = new ArrayList<String>();
			List<String> titles = new ArrayList<String>();
			
			//additinal spaces
			String extra = "                                                                                                                                                                                                                   ";
			// Iterate through object to get details
			for (int i = 0; i < list.size(); i++) {
				
                SyndEntry entry = (SyndEntry) list.get(i);
				String display = "";
				String link = null;
				//get title
				display += entry.getTitle() + extra;
				titles.add(display);
				//get link
				link = entry.getLink();
				links.add(link);

			}
			ArrayList<List<String>> titlelink = new ArrayList();
			titlelink.add((ArrayList<String>) titles);
			titlelink.add((ArrayList<String>) links);

			return titlelink;
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return null;
	}
}